﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Ninject;
using Ninject.Syntax;
using Tss.MagicWeb.Domain.Abstract;
using Tss.MagicWeb.Domain.Concrete;

namespace Tss.MagicWeb.Domain.Infrastructure
{
    public class NinjectKernel : IDependencyResolver
    {
        private IKernel kernel;

        public NinjectKernel()
        {
            kernel = new StandardKernel();
            AddBindings();
        }

        public void AddBindings()
        {
            kernel.Bind<IUserRepository>().To<UserRepository>();
            kernel.Bind<IBasePhysicalCardRepository>().To<BasePhysicalCardRepository>();
            kernel.Bind<IDeckItemRepository>().To<DeckItemRepository>();
            kernel.Bind<IDeckRepository>().To<DeckRepository>();
        }

        public object GetService(Type serviceType)
        {
            return kernel.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return kernel.GetAll(serviceType);
        }

        public IKernel Kernel
        {
            get
            {
                return kernel;
            }
        }

        public IBindingToSyntax<T> Bind<T>()
        {
            return kernel.Bind<T>();
        }

        public void RegisterInstancePerThread<T>(T instance)
        {
            kernel.Bind<T>().ToConstant(instance).InThreadScope();
        }
    }
}
